package ws;

import java.util.List;

import javax.ws.rs.*;
import javax.ws.rs.core.*;

import com.sun.xml.internal.bind.v2.runtime.Name;

import model.DanhMucModel;
import entities.Danhmuc;


@Path("danhmuc")
public class DanhMucResful {
	
	@GET
	@Path("findxml")
	@Produces(MediaType.APPLICATION_XML)
	public Danhmuc find(){
		DanhMucModel dmm = new DanhMucModel();
		return dmm.find(5);
	}
	
	@GET
	@Path("findallxml")
	@Produces(MediaType.APPLICATION_XML)
	public List<Danhmuc> findAll(){
		DanhMucModel dmm = new DanhMucModel();
		return dmm.findAll();
	}
	
	@GET
	@Path("findjson")
	@Produces(MediaType.APPLICATION_JSON)
	public Danhmuc findjson(){
		DanhMucModel dmm = new DanhMucModel();
		return dmm.find(5);
	}
	
	@GET
	@Path("findalljson")
	@Produces(MediaType.APPLICATION_JSON)
	public List<Danhmuc> findAlljson(){
		DanhMucModel dmm = new DanhMucModel();
		return dmm.findAll();
	}
	
	@POST
	@Consumes({"application/xml","application/json"})
	public boolean create(Danhmuc dm){
		try {
			DanhMucModel dmm = new DanhMucModel();
			dmm.create(dm);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
	}
	
	@DELETE
	@Path("delete/{id}")
	public boolean delete(@PathParam(value="id")Integer id){
		try {
			DanhMucModel dmm = new DanhMucModel();
			dmm.delete(dmm.find(id));
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
	}
	
	@PUT
	@Consumes({"application/xml","application/json"})
	public boolean edit(Danhmuc dm){
		try {
			DanhMucModel dmm = new DanhMucModel();
			dmm.update(dm);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
	}
}
